#include "cuserservice.h"
#include "Core/tcpclient.h"
CUserService::CUserService(QObject *parent) : QObject(parent)
{

}

void CUserService::RecvRegisterRespond(std::shared_ptr<PDU>recvBag)
{
    char * InfoChar=new char[100000]{'\0'};
    memcpy(InfoChar,(char*)recvBag.get()+sizeof(PDU),recvBag->MsgLength);
    QJsonDocument dc=QJsonDocument::fromJson(QString(InfoChar).toUtf8());
    if(dc.isNull())return;
    if(!dc.isObject())return;
    delete [] InfoChar;
    InfoChar=nullptr;

    QJsonObject JsonInfo=dc.object();
    qDebug()<<JsonInfo;
    int Status=dc["LOGINSTATUS"].toInt();
    QString Say=dc["LOGINSAY"].toString();
    //QString UserName=dc["USER"].toString();


    QString RespondInfo=recvBag->caData;
    if(Status==1){
        emit EmitRegisterSuccess(Say);
    }else if(Status==0){
        emit EmitRegisterFail(Say);
    }
}

void CUserService::RecvLoginRespond(std::shared_ptr<PDU>recvBag)
{
    char * InfoChar=new char[100000]{'\0'};
    memcpy(InfoChar,(char*)recvBag.get()+sizeof(PDU),recvBag->MsgLength);
    QJsonDocument dc=QJsonDocument::fromJson(QString(InfoChar).toUtf8());
    if(dc.isNull())return;
    if(!dc.isObject())return;
    delete [] InfoChar;
    InfoChar=nullptr;

    QJsonObject JsonInfo=dc.object();
    qDebug()<<JsonInfo;
    int Status=dc["LOGINSTATUS"].toInt();
    QString Say=dc["LOGINSAY"].toString();
    QString UserName=dc["USER"].toString();
    if(Status==0){
        emit EmitLoginFail(Say);
    }else if(Status==1){
        TcpClient::get_instance()
                ->SetUserName(UserName);
        emit EmitLoginSuccess(Say);
    }
}
